package com.by.zhdoctor.web.rest;

import com.by.zhdoctor.domain.BingMing;
import com.by.zhdoctor.repository.BingMingRepository;
import com.by.zhdoctor.web.rest.util.HeaderUtil;
import com.by.zhdoctor.web.rest.util.PaginationUtil;
import com.codahale.metrics.annotation.Timed;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.inject.Inject;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
import java.util.Optional;

/**
 * REST controller for managing BingMing.
 */
@RestController
@RequestMapping("/api")
public class BingMingResource {

    private final Logger log = LoggerFactory.getLogger(BingMingResource.class);

    @Inject
    private BingMingRepository bingMingRepository;

    /**
     * POST  /bing-mings : Create a new bingMing.
     *
     * @param bingMing the bingMing to create
     * @return the ResponseEntity with status 201 (Created) and with body the new bingMing, or with status 400 (Bad Request) if the bingMing has already an ID
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PostMapping("/bing-mings")
    @Timed
    public ResponseEntity<BingMing> createBingMing(@RequestBody BingMing bingMing) throws URISyntaxException {
        log.debug("REST request to save BingMing : {}", bingMing);
        if (bingMing.getId() != null) {
            return ResponseEntity.badRequest().headers(HeaderUtil.createFailureAlert("bingMing", "idexists", "A new bingMing cannot already have an ID")).body(null);
        }
        BingMing result = bingMingRepository.save(bingMing);
        return ResponseEntity.created(new URI("/api/bing-mings/" + result.getId()))
            .headers(HeaderUtil.createEntityCreationAlert("bingMing", result.getId().toString()))
            .body(result);
    }

    /**
     * PUT  /bing-mings : Updates an existing bingMing.
     *
     * @param bingMing the bingMing to update
     * @return the ResponseEntity with status 200 (OK) and with body the updated bingMing,
     * or with status 400 (Bad Request) if the bingMing is not valid,
     * or with status 500 (Internal Server Error) if the bingMing couldnt be updated
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PutMapping("/bing-mings")
    @Timed
    public ResponseEntity<BingMing> updateBingMing(@RequestBody BingMing bingMing) throws URISyntaxException {
        log.debug("REST request to update BingMing : {}", bingMing);
        if (bingMing.getId() == null) {
            return createBingMing(bingMing);
        }
        BingMing result = bingMingRepository.save(bingMing);
        return ResponseEntity.ok()
            .headers(HeaderUtil.createEntityUpdateAlert("bingMing", bingMing.getId().toString()))
            .body(result);
    }

    /**
     * GET  /bing-mings : get all the bingMings.
     *
     * @param pageable the pagination information
     * @return the ResponseEntity with status 200 (OK) and the list of bingMings in body
     * @throws URISyntaxException if there is an error to generate the pagination HTTP headers
     */
    @GetMapping("/bing-mings")
    @Timed
    public ResponseEntity<List<BingMing>> getAllBingMings(Pageable pageable)
        throws URISyntaxException {
        log.debug("REST request to get a page of BingMings");
        Page<BingMing> page = bingMingRepository.findAll(pageable);
        HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/api/bing-mings");
        return new ResponseEntity<>(page.getContent(), headers, HttpStatus.OK);
    }


    /**
     * GET  /bing-mings : get all the BingMings.
     *
     * @return the ResponseEntity with status 200 (OK) and the list of hospitals in body
     */
    @GetMapping("/bing-mings/All")
    @Timed
    public List<BingMing> getAllBingMings() {
        log.debug("REST request to get all BingMings");
        List<BingMing> BingMings = bingMingRepository.findAll();
        return BingMings;
    }

    /**
     * GET  /bing-mings/:id : get the "id" bingMing.
     *
     * @param id the id of the bingMing to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the bingMing, or with status 404 (Not Found)
     */
    @GetMapping("/bing-mings/{id}")
    @Timed
    public ResponseEntity<BingMing> getBingMing(@PathVariable Long id) {
        log.debug("REST request to get BingMing : {}", id);
        BingMing bingMing = bingMingRepository.findOne(id);
        return Optional.ofNullable(bingMing)
            .map(result -> new ResponseEntity<>(
                result,
                HttpStatus.OK))
            .orElse(new ResponseEntity<>(HttpStatus.NOT_FOUND));
    }

    /**
     * DELETE  /bing-mings/:id : delete the "id" bingMing.
     *
     * @param id the id of the bingMing to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @DeleteMapping("/bing-mings/{id}")
    @Timed
    public ResponseEntity<Void> deleteBingMing(@PathVariable Long id) {
        log.debug("REST request to delete BingMing : {}", id);
        bingMingRepository.delete(id);
        return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert("bingMing", id.toString())).build();
    }

}
